package com.lfz.rabbit.sync;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;

@Component
public class RPCClient {

    @Autowired
    AmqpTemplate amqpTemplate;

    /**
     * 同步执行任务，一条一条执行
     * @param message 发送的消息字符串
     * @param syncRpcQueueName 队列名称
     * @return rpc返回的处理结果
     */
    public String send(String message, String syncRpcQueueName) {
        return (String) amqpTemplate.convertSendAndReceive(syncRpcQueueName, message);
    }

    /**
     * 异步执行任务，同时执行多条任务
     * @param message 发送的消息字符串
     * @param syncRpcQueueName 队列名称
     * @return rpc返回的处理结果
     */
    @Async
    public Future<String> sendAsync(String message, String syncRpcQueueName) {
        return new AsyncResult<>(send(message, syncRpcQueueName));
    }
}